package com.realbrand.net.realbrandpay.ui.activity.wallet.personer.adress;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lljjcoder.citypickerview.widget.CityPicker;
import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.base.BaseActivity;
import com.realbrand.net.realbrandpay.bean.AdressBean;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.zxing.util.L;

import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/5/14.
 */

public class UpDataAdressActivity extends BaseActivity implements IViewAddAdress {


    @BindView(R.id.compileAdress_toolbar_save)
    TextView compileAdressToolbarSave;
    @BindView(R.id.compileAdress_toolbar)
    Toolbar compileAdressToolbar;
    @BindView(R.id.compileAdress_et_consignee)
    EditText compileAdressEtConsignee;
    @BindView(R.id.compileAdress_et_phone)
    EditText compileAdressEtPhone;
    @BindView(R.id.compileAdress_tv_province)
    TextView compileAdressTvProvince;
    @BindView(R.id.compileAdress_tv_city)
    TextView compileAdressTvCity;
    @BindView(R.id.compileAdress_tv_district)
    TextView compileAdressTvDistrict;
    @BindView(R.id.compileAdress_linear)
    LinearLayout compileAdressLinear;
    @BindView(R.id.compileAdress_et_detailedAdress)
    EditText compileAdressEtDetailedAdress;

    PersenterAddAdress persenterUpDataAddress;

    private String addressId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        compileAdressToolbar.setTitle("");
        compileAdressToolbar.setNavigationIcon(R.mipmap.ic_back);
        setSupportActionBar(compileAdressToolbar);
        compileAdressToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        init();
        setOnClick();
    }

    private void init() {
        Bundle bundle = getIntent().getExtras();
        int position = bundle.getInt("position");
        AdressBean bean = (AdressBean) bundle.getSerializable("addressBean");
        compileAdressEtConsignee.setText(bean.getAddresseeName().get(position));
        compileAdressEtPhone.setText(bean.getTelephone().get(position));
        compileAdressTvProvince.setText(bean.getProvince().get(position));
        compileAdressTvCity.setText(bean.getRegionCity().get(position));
        compileAdressTvDistrict.setText(bean.getCountyDistrict().get(position));
        compileAdressEtDetailedAdress.setText(bean.getDetailedAddress().get(position));
        addressId = bean.getAddressId().get(position);
        persenterUpDataAddress = new PersenterAddAdress();
        persenterUpDataAddress.attachView(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.addadress_layout;
    }

    @Override
    protected void setOnClick() {
        compileAdressToolbarSave.setOnClickListener(this);
        compileAdressLinear.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //保存
            case R.id.compileAdress_toolbar_save:
                String consignee = compileAdressEtConsignee.getText().toString();
                String phone = compileAdressEtPhone.getText().toString();
                String detatledAdress = compileAdressEtDetailedAdress.getText().toString();
                String province = compileAdressTvProvince.getText().toString();
                String city = compileAdressTvCity.getText().toString();
                String district = compileAdressTvDistrict.getText().toString();
                if (consignee.equals("") || phone.equals("") || detatledAdress.equals("") || province.equals("") || city.equals("") || district.equals("")) {
                    Static.toast(this, "请完善所有信息");
                } else {
                    ArrayList<String> keyList = new ArrayList<>();
                    keyList.add("AddressId");
                    keyList.add("AddresseeName");
                    keyList.add("Telephone");
                    keyList.add("Province");
                    keyList.add("RegionCity");
                    keyList.add("CountyDistrict");
                    keyList.add("DetailedAddress");
                    keyList.add("Postalcode");
                    ArrayList<String> valueList = new ArrayList<>();
                    valueList.add(addressId);
                    valueList.add(consignee);
                    valueList.add(phone);
                    valueList.add(province);
                    valueList.add(city);
                    valueList.add(district);
                    valueList.add(detatledAdress);
                    valueList.add("000000");
                    String params = Static.getString(keyList, valueList);
                    try {
                        String encode = URLEncoder.encode(params, "utf-8");
                        JSONObject upDataAdressJson = new JSONObject();
                        upDataAdressJson.put("METHOD", "UpdateAddressInfo");
                        upDataAdressJson.put("SOURCE", "UpDataAdressActivity");
                        upDataAdressJson.put("CREDENTIALS", "0");
                        upDataAdressJson.put("TERMINAL", "0");
                        upDataAdressJson.put("INDEX", Static.getDay());
                        upDataAdressJson.put("DATA", encode);
                        persenterUpDataAddress.updataAddress(upDataAdressJson.toString());
                        L.d(L.TAG, "更新地址传入的参数" + upDataAdressJson.toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            //选择地址
            case R.id.compileAdress_linear:
                //判断输入法的状态
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm.isActive()) {
                    imm.hideSoftInputFromWindow(compileAdressLinear.getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    chooseAdress();//调用CityPicker选取区域
                }
                break;
        }
    }

    /**
     * 选择地址
     */
    private void chooseAdress() {
        CityPicker cityPicker = new CityPicker.Builder(UpDataAdressActivity.this)
                .textSize(14)
                .title("所在地区")
                .confirTextColor("#F96F29")
                .cancelTextColor("#F96F29")
                .build();
        cityPicker.show();
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... citySelected) {
                compileAdressTvProvince.setText(citySelected[0]);
                compileAdressTvCity.setText(citySelected[1]);
                compileAdressTvDistrict.setText(citySelected[2]);
            }
        });
    }

    @Override
    public void showResult(String result) {
        L.d(L.TAG, "更新地址结果" + result);
        try {
            JSONObject resultJson = new JSONObject(result);
            String returncode = resultJson.getString("RETURNCODE");
            if (returncode.equals("200")) {
                Intent intent = new Intent(UpDataAdressActivity.this, ManagerAdressActivity.class);
                intent.putExtra("addAdressResult", returncode);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showFailMsg(String msg) {
        Static.toast(UpDataAdressActivity.this, msg);
    }

    @Override
    public void showError() {
        Static.toast(UpDataAdressActivity.this, "网络异常");
    }

    @Override
    public Context getContext() {
        return UpDataAdressActivity.this;
    }
}
